
package kaizen.jdbc;

import java.io.IOException;
import java.io.PrintStream;

import java.sql.Connection;
import java.sql.ResultSetMetaData;

import java.util.Properties;
import java.util.Vector;

import javax.servlet.http.HttpServlet;

import kaizen.nomweb.Historico;
import kaizen.nomweb.utilerias;

// Referenced classes of package kaizen.jdbc:
//            cMgr

public class DbAppUnivC extends HttpServlet
{

  public DbAppUnivC()
  {
  }

  public static void closeConnJSP() throws IOException
  {
    cMgr.close(exConnJSP);
  }

  public static void commitJSP()
  {
    cMgr.commit(exConnJSP);
  }

  public static Vector execQuery(String sSql) throws IOException
  {
    Properties prop = utilerias.loadProperties("/kaizen/general.properties");
    if (!binicio)
      init1();
    Vector vRecord = null;
    String ls_driver = prop.getProperty("driverUniv");
    String ls_url = prop.getProperty("urlUniv");
    String ls_user = prop.getProperty("userUniv");
    String ls_password = prop.getProperty("passwordUniv");
    String nombre_conn = "connQuery_" + String.valueOf(i_query);
    boolean bquery = 
      cMgr.add(nombre_conn, ls_driver, ls_url, ls_user, ls_password);
    String arch_log = prop.getProperty("log_Unic_execQuery");
    sSql = utilerias.validaComillasSeguidas(sSql);
    Historico log = new Historico(arch_log, true);
    if (!sSql.startsWith("SELECT us_usuario, us_passwd FROM gentus where us_cvesis ="))
      log.agregar(utilerias.fechaHoy("MMMMM.dd GGG hh:mm aaa") + ", " + 
                  String.valueOf(i_query) + " : " + sSql);
    else
      System.out.println(".:.: Se estan verificando los passwords");
    String cursor = "query_" + String.valueOf(i_query);
    try
    {
      cMgr.executeQuery(nombre_conn, cursor, sSql);
      vRecord = cMgr.getAllRecords(nombre_conn, cursor);
      cMgr.close("con1");
    } catch (Exception e)
    {
      String err = "existio un error al correr el query " + e.toString();
      log.agregar(err);
      System.out.println(err);
      bExecQuery = false;
    }
    if (vRecord == null)
      vRecord = new Vector();
    log.cerrar();
    cMgr.removeCursor(nombre_conn, cursor);
    cMgr.close(nombre_conn);
    i_query++;
    return vRecord;
  }

  public static Vector execQuery(String sSql, String ls_driver, String ls_url, 
                                 String ls_user, 
                                 String ls_password) throws IOException
  {
    Vector vRecord = null;
    boolean bValor = cMgr.add("con1", ls_driver, ls_url, ls_user, ls_password);
    try
    {
      cMgr.executeQuery("con1", "query", sSql);
      vRecord = cMgr.getAllRecords("con1", "query");
      cMgr.close("con1");
    } catch (Exception _ex)
    {
    }
    if (vRecord == null)
      vRecord = new Vector();
    return vRecord;
  }

  public static boolean execStatement(String sSql) throws IOException
  {
    Properties prop = utilerias.loadProperties("/kaizen/general.properties");
    String ls_driver = prop.getProperty("driverUniv");
    String ls_url = prop.getProperty("urlUniv");
    String ls_user = prop.getProperty("userUniv");
    String ls_password = prop.getProperty("passwordUniv");
    String arch_log = prop.getProperty("log_Unic_execStatement");
    sSql = utilerias.validaComillasSeguidas(sSql);
    Historico log = new Historico(arch_log, true);
    log.agregar(utilerias.fechaHoy("MMMMM.dd GGG hh:mm aaa") + " " + sSql);
    log.cerrar();
    return execStatement(sSql, ls_driver, ls_url, ls_user, ls_password);
  }

  public static boolean execStatement(String sSql, String ls_driver, 
                                      String ls_url, String ls_user, 
                                      String ls_password) throws IOException
  {
    boolean bValor = cMgr.add("con2", ls_driver, ls_url, ls_user, ls_password);
    boolean salida = false;
    cMgr.commit("con2");
    if (cMgr.executeStatement("con2", sSql))
    {
      cMgr.commit("con2");
      salida = true;
    } else
    {
      cMgr.rollback("con2");
    }
    cMgr.close("con2");
    return salida;
  }

  public static String execStatementForms(String sSql) throws IOException
  {
    Properties prop = utilerias.loadProperties("/kaizen/general.properties");
    String ls_driver = prop.getProperty("driverUniv");
    String ls_url = prop.getProperty("urlUniv");
    String ls_user = prop.getProperty("userUniv");
    String ls_password = prop.getProperty("passwordUniv");
    boolean bValor = 
      cMgr.add("con27", ls_driver, ls_url, ls_user, ls_password);
    cMgr.commit("con2");
    String arch_log = prop.getProperty("log_Unic_execStatement");
    sSql = utilerias.validaComillasSeguidas(sSql);
    Historico log = new Historico(arch_log, true);
    log.agregar(utilerias.fechaHoy("MMMMM.dd GGG hh:mm aaa") + " " + sSql);
    log.cerrar();
    String err = cMgr.executeStatementForms("con27", sSql);
    if (err.length() == 0)
      cMgr.commit("con27");
    else
      cMgr.rollback("con27");
    cMgr.close("con27");
    return err;
  }

  public static boolean execStatementJSP(String sSql) throws IOException
  {
    Properties prop = utilerias.loadProperties("/kaizen/general.properties");
    String ls_driver = prop.getProperty("driverUniv");
    String ls_url = prop.getProperty("urlUniv");
    String ls_user = prop.getProperty("userUniv");
    String ls_password = prop.getProperty("passwordUniv");
    boolean flag;
    if (!cMgr.getStatus(exConnJSP))
      flag = cMgr.add(exConnJSP, ls_driver, ls_url, ls_user, ls_password);
    String arch_log = prop.getProperty("log_Unic_execStatement");
    sSql = utilerias.validaComillasSeguidas(sSql);
    Historico log = new Historico(arch_log, true);
    log.agregar(utilerias.fechaHoy("MMMMM.dd GGG hh:mm aaa") + " " + sSql);
    log.cerrar();
    boolean salida = cMgr.executeStatement(exConnJSP, sSql);
    return salida;
  }

  public static Connection getStoreConn() throws IOException
  {
    if (!binicio)
      init1();
    System.out.println("Pasando la conexion");
    return cMgr.getConnection(sStoreConn);
  }

  public static void init1() throws IOException
  {
    i_query = 0;
    store_bValor = false;
    binicio = true;
  }

  public static ResultSetMetaData metaExecQuery(String sSql) throws IOException
  {
    Properties prop = utilerias.loadProperties("/kaizen/general.properties");
    ResultSetMetaData resultsetmetadata = null;
    String ls_driver = prop.getProperty("driverUniv");
    String ls_url = prop.getProperty("urlUniv");
    String ls_user = prop.getProperty("userUniv");
    String ls_password = prop.getProperty("passwordUniv");
    boolean bValor = cMgr.add("con1", ls_driver, ls_url, ls_user, ls_password);
    String arch_log = prop.getProperty("log_Unic_execQuery");
    sSql = utilerias.validaComillasSeguidas(sSql);
    Historico log = new Historico(arch_log, true);
    log.agregar(sSql);
    log.cerrar();
    try
    {
      cMgr.executeQuery("con1", "query", sSql);
      resultsetmetadata = cMgr.getMetaData("con1", "query");
      cMgr.close("con1");
    } catch (Exception _ex)
    {
    }
    return resultsetmetadata;
  }

  public static ResultSetMetaData metaExecQuery(String sSql, String ls_driver, 
                                                String ls_url, String ls_user, 
                                                String ls_password) throws IOException
  {
    ResultSetMetaData resultsetmetadata = null;
    boolean bValor = cMgr.add("con1", ls_driver, ls_url, ls_user, ls_password);
    try
    {
      cMgr.executeQuery("con1", "query", sSql);
      resultsetmetadata = cMgr.getMetaData("con1", "query");
      cMgr.close("con1");
    } catch (Exception _ex)
    {
    }
    return resultsetmetadata;
  }

  public static void rollBackJSP()
  {
    cMgr.rollback(exConnJSP);
  }

  public static boolean setStoreConn() throws IOException
  {
    System.out.println("Estableciendo conexion al store");
    if (!store_bValor)
    {
      Properties prop = utilerias.loadProperties("/kaizen/general.properties");
      String ls_driver = prop.getProperty("driverUniv");
      String ls_url = prop.getProperty("urlUniv");
      String ls_user = prop.getProperty("userUniv");
      String ls_password = prop.getProperty("passwordUniv");
      store_bValor = 
          cMgr.add(sStoreConn, ls_driver, ls_url, ls_user, ls_password);
    }
    return store_bValor;
  }

  public static boolean binicio = false;
  private static int i_query;
  private static boolean bExecQuery;
  private static boolean store_bValor;
  private static String sStoreConn = "connStore";
  private static String exConnJSP = "exConnJSP";
  private static String nombre_conn = "lite";

}
